package admin

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"shangyuapi/data_source"
	"shangyuapi/models"
	"time"
)

type CaozuosRBody struct {
	Data       []models.Caozuo `json:"data"`
	PageNo     int             `json:"pageNo"`
	PageSize   int             `json:"pageSize"`
	TotalCount int64           `json:"totalCount"`
	TotalPage  int64           `json:"totalPage"`
}

func Caozuoinfo(ctx *gin.Context) {
	request := JiaochengsBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	var caozuo models.Caozuo
	data_source.Db.Where("id = ?", id).First(&caozuo)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    caozuo,
		"msg":     "ok",
	})
	return
}
func Caozuodel(ctx *gin.Context) {
	request := JiaochengsBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	data_source.Db.Where("id = ?", id).Delete(&models.Caozuo{})
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Caozuoupdate(ctx *gin.Context) {
	request := JiaochengsBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	title := request.Title
	content := request.Content
	jc := models.Caozuo{
		Title:   title,
		Content: content,
	}
	data_source.Db.Where("id = ?", id).Updates(&jc)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Caozuoadd(ctx *gin.Context) {
	request := JiaochengsBody{}
	ctx.ShouldBind(&request)
	content := request.Content
	title := request.Title
	jc := models.Caozuo{
		Content: content,
		Title:   title,
		Addtime: time.Now().Unix(),
	}
	data_source.Db.Create(&jc)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Caozuos(ctx *gin.Context) {
	request := JiaochengsBody{}
	ctx.ShouldBind(&request)
	currentPage := request.PageNo
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Caozuo
	res := data_source.Db
	res.Model(&models.Caozuo{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := CaozuosRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

type ShensusRBody struct {
	Data       []models.Shensusql `json:"data"`
	PageNo     int                `json:"pageNo"`
	PageSize   int                `json:"pageSize"`
	TotalCount int64              `json:"totalCount"`
	TotalPage  int64              `json:"totalPage"`
}

func Shensus(ctx *gin.Context) {
	request := JiaochengsBody{}
	ctx.ShouldBind(&request)
	currentPage := request.PageNo
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Shensusql
	res := data_source.Db.Model(&models.Shensu{}).Select("shensu.*, hongniang.name").Joins("left join hongniang on hongniang.id = shensu.hongniangid")
	res.Model(&models.Shensu{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := ShensusRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

type JubaosRBody struct {
	Data       []models.Jubaosql `json:"data"`
	PageNo     int               `json:"pageNo"`
	PageSize   int               `json:"pageSize"`
	TotalCount int64             `json:"totalCount"`
	TotalPage  int64             `json:"totalPage"`
}

func Jubaos(ctx *gin.Context) {
	request := JiaochengsBody{}
	ctx.ShouldBind(&request)
	currentPage := request.PageNo
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Jubaosql
	res := data_source.Db.Model(&models.Jubao{}).Select("jubao.*, hongniang.name").Joins("left join hongniang on hongniang.id = jubao.hongniangid")
	res.Model(&models.Jubao{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := JubaosRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}
